package com.zhoux.mongo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zhoux.mongo.util.JsonUtil;
import com.zhoux.mongo.util.StringUtil;
import com.zhoux.mongo.util.XmlUtil;



public class Mongodb {
	private static Log log=LogFactory.getLog(Mongodb.class);
	private static final String xmlHeader="<?xml version=\"1.0\" encoding=\"charset\"?>";
	private static String charset="UTF-8";
	
	@SuppressWarnings("static-access")
	public Boolean createDataBase(String dataBaseName,String charset) throws Exception{
		if(charset!=null){
			this.charset=charset;
		}
		return createDataBase(dataBaseName);
	}
	/**
	 * 鍒涘缓涓�涓暟鎹簱
	 * @param dataBaseName 闇�瑕佸垱寤烘暟鎹簱鐨勫悕瀛�
	 * @return 杩斿洖鍒涘缓缁撴灉
	 * @throws Exception 
	 */
	private Boolean createDataBase(String dataBaseName) throws Exception{
		dataBaseName=StringUtil.formatFileName(dataBaseName);
		File file=new File(dataBaseName);
		if(file.exists()&&file.isFile())
		{
			log.error("this database is exists!");
			throw new Exception();
		}
		OutputStreamWriter fout=null;
		try {
			fout = new OutputStreamWriter(new FileOutputStream(dataBaseName),charset);
			fout.write(xmlHeader.replaceAll("charset", charset));
			fout.write("\r\n");
			fout.write("<database>");
			fout.write("\r\n");
			fout.write("</database>");
			return true;
		} catch (FileNotFoundException e) {
			log.error("this file is conn't found!");
		}catch (IOException e) {
			log.error("init database is failure");
		}finally{
			if(fout!=null)
			{
				try {
					fout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	/**
	 * 鍒犻櫎涓�涓暟鎹簱
	 * @param dataBaseName
	 * @return
	 * @throws Exception 
	 */
	public Boolean deleteDataBase(String dataBaseName) throws Exception{
		dataBaseName=StringUtil.formatFileName(dataBaseName);
		File file=new File(dataBaseName);
		if(!file.exists())
		{
			log.error("this database is not exists:"+dataBaseName);
			return false;
		}
		file.deleteOnExit();
		return true;
	}
	/**
	 * 鍒涘缓涓�涓〃
	 * @param dataBaseName
	 * @param tableName
	 * @return
	 */
	public Boolean createTable(String dataBaseName,String tableName){
		//鍒ゆ柇搴撴槸鍚﹀瓨鍦�
		boolean flag=false;
		if(XmlUtil.decideTable(dataBaseName, tableName)){
			log.error(tableName+" is exists in the database" );
			return flag;
		}
		try {
			XmlUtil.newTable(dataBaseName, tableName);
			flag=true;
		}catch (Exception e) {
			log.error("鍓靛缓琛ㄥけ鏁楋紒");
		}
		return flag;
	}
	/**
	 * 鍒犻櫎涓�寮犺〃
	 * @param dataBaseName
	 * @param tableName
	 * @return
	 */
	public Boolean deleteTable(String dataBaseName,String tableName){
		return XmlUtil.dropTable(dataBaseName, tableName);
	}
	/**
	 * 鎻掑叆涓�涓狣ocument
	 * @param dataBaseName
	 * @param tableName
	 * @param json
	 * @return
	 */
	public Boolean insertDocument(String dataBaseName,String tableName,Object obj){
		String json=JsonUtil.objectToString(obj);
		return XmlUtil.insertDocument(dataBaseName, tableName, json);
	}
	/**
	 * 鏍规嵁鏉′欢鏇存柊涓�涓狣ocument
	 * @param dataBaseName
	 * @param tableName
	 * @param json
	 * @return
	 */
	public Boolean updateDocument(String dataBaseName,String tableName,Map updateMap,String id){
		return XmlUtil.updateDocument(dataBaseName, tableName, updateMap, id);
	}
	
	/**
	 * 鏍规嵁鏉′欢鍒犻櫎涓�涓猟ocument
	 * @param dataBaseName
	 * @param tableName
	 * @param json
	 * @return
	 */
	public Boolean deleteDocument(String dataBaseName,String tableName,String id){
		return XmlUtil.deleteDocument(dataBaseName, tableName, id);
	}
	/**
	 * 鏍规嵁鏉′欢鏌ヨdocument
	 * @param dataBaseName
	 * @param tableName
	 * @param json
	 * @return
	 */
	public String queryDocument(String dataBaseName,String tableName,String id){
		Map map=XmlUtil.queryDocumentById(dataBaseName, tableName, id);
		if(map.isEmpty())
			return "";
		return map.toString();
	}
	/**
	 * 鏍规嵁鏉′欢鏌ヨ锛岃繑鍥炵鍚堟潯浠剁殑闆嗗悎
	 * @param dataBaseName
	 * @param tableName
	 * @param json
	 * @return
	 */
	public List<HashMap> queryDocumentList(String dataBaseName,String tableName){
		
		return XmlUtil.queryDpcumentList(dataBaseName, tableName);
	}
	
	
	
public static void main(String[] args) throws Exception {
	
	Mongodb db=new Mongodb();
	//db.createDataBase("zhoux",null);
	//db.deleteDataBase("zhoux.xml");
//	db.createTable("zhoux", "zhoux");
//	
	Map map =new HashMap();
	map.put("name", "zly");
	map.put("age", 18);
//	db.insertDocument("zhoux", "zhoux",map);
	
//	db.deleteDocument("zhoux", "zhoux", "1489068616614");
	List<HashMap> list=db.queryDocumentList("zhoux", "zhoux");
	for (HashMap hashMap : list) {
		System.out.println(hashMap.toString());
	}
//	db.updateDocument("zhoux", "zhoux", map, "1489069844373");
	
}
}
